APP_DIR = ../../app
include $(APP_DIR)/Rules.make
include $(APP_DIR)/odin_inc

NAME = $(notdir $(CURDIR))
EXE_NAME = $(NAME).out

#components 模块编译
LIB_MODULES := $(patsubst ../components/rules/%/rules.mak,%,$(wildcard ../components/rules/*/rules.mak))
LIB_MODULES += HisiEncGetStream HisiDecSendStream HisiDecEnc mpp
LIBS_DIR = $(APP_DIR)/components
LIBS_SRC_DIR = $(LIBS_DIR)/src

#所依赖的本地库文件，位于component目录下，
LOCAL_INC_DIR =  $(APP_DIR)/components/include
LOCAL_LIB_DIR =  $(APP_DIR)/components/lib
HIMPP_LIB_DIR =  $(APP_DIR)/components/src/mpp/out/linux/multi-core/lib
HIMPP_INC_DIR =  -I$(APP_DIR)/components/src/mpp/out/linux/multi-core/include \
								 -I$(APP_DIR)/components/src/mpp/sample/audio/adp

#所有的Odin依赖库在此添加
LOCAL_LIBS = HisiEncGetStream HisiDecSendStream HisiDecEnc mpp
LOCAL_STATIC_LIBS = $(foreach n,$(LOCAL_LIBS),$(LOCAL_LIB_DIR)/lib$(n).a)
HIMPP_LIBS := _hiae isp _hidehaze _hidrc _hildci sns_imx477 sns_imx290 sns_imx334 sns_imx334_slave sns_imx277_slvs
HIMPP_LIBS += sns_sharp8k _hiawb _hicalcflicker mpi hdmi dpu_rect dpu_match hiavslut pos_query securec upvqe dnvqe VoiceEngine
HIMPP_STATIC_LIBS = $(foreach n,$(HIMPP_LIBS),$(HIMPP_LIB_DIR)/lib$(n).a)

EXE_DIR = $(APP_DIR)/_install/app_driver/
CFLAGS =  -I. -I../include -I$(LOCAL_INC_DIR) -Wall -g  -o2 #-Werror
CFLAGS += $(HIMPP_INC_DIR)

#添加ODIN的头文件路径
CFLAGS +=  $(ODIN_INC_C_FLAG)
CFLAGS += -I$(OPEN_LIB_DIR)/srt/$(COMPILER)/include/srt

LDFLAGS = -Xlinker "-(" -L$(LOCAL_LIB_DIR) $(LOCAL_STATIC_LIBS) -Xlinker "-)"
LDFLAGS += $(ODIN_STATIC_LIBS) $(ODIN_DIR)/open_lib/$(JSON_C_VERSION)/$(COMPILER)/lib/libjson-c.a
LDFLAGS += $(OPEN_LIB_DIR)/zlib/$(COMPILER)/lib/libzlib.a
LDFLAGS += -Xlinker "-(" -L$(HIMPP_LIB_DIR) $(HIMPP_STATIC_LIBS) -Xlinker "-)"
LDFLAGS += -lstdc++ 
LDFLAGS += $(OPEN_LIB_DIR)/srt/$(COMPILER)/lib/libsrt.a
LDFLAGS += $(OPEN_LIB_DIR)/openssl/$(COMPILER)/lib/libssl.a
LDFLAGS += $(OPEN_LIB_DIR)/openssl/$(COMPILER)/lib/libcrypto.a
LDFLAGS += -lpthread -lrt -lm -ldl -rdynamic

objects := $(patsubst %.c,%.o,$(wildcard *.c))

all: core
	$(CP) $(EXE_NAME) $(EXE_DIR)
	
core : $(objects)
	rm -f ./$(EXE_NAME)
	$(COMPILE_PREFIX)gcc  $(objects) -o $(EXE_NAME) $(LDFLAGS) 
	#$(COMPILE_PREFIX)objdump -p $@ | grep NEEDED > NEED.dep
	
$(objects): %.o: %.c
	$(COMPILE_PREFIX)gcc -c $(CFLAGS) $< -o $@
	
install:
	cp $(EXE_NAME) $(EXE_DIR)
	cp $(EXE_NAME) debug
	@echo $(EXE_DIR)

clean:
	rm -f ./$(EXE_NAME)
	rm -f *.o

distclean: clean
	rm -f *.o
	
components:
	@echo -e "***********build libs:\n$(LIB_MODULES)\n************"
	@for dir in $(LIB_MODULES); do  $(MAKE)  -s -C$(LIBS_SRC_DIR)/$$dir BUILD_TYPE=release  && $(MAKE)  -C$(LIBS_SRC_DIR)/$$dir install||exit "$$?"; done
	@for dir in $(LIB_MODULES); do  $(MAKE)  -s -C$(LIBS_SRC_DIR)/$$dir install||exit "$$?"; done
		
components_clean:
	@echo -e "***********clean libs:\n$(LIB_MODULES)\n************"
	@for dir in $(LIB_MODULES); do  $(MAKE)  -s -C$(LIBS_SRC_DIR)/$$dir clean||exit "$$?"; done